package code.lewdy.web.request;

import java.io.UnsupportedEncodingException;

import javax.servlet.http.HttpServletRequest;

/**
 * 扩展的HttpServletRequest
 * @author liuding,2012-4-25
 *
 * $LastChangedDate$
 * $Author$ 
 */
public class ExtRequest
{
    private HttpServletRequest request;

    public ExtRequest(HttpServletRequest request)
    {
        this.request = request;
        try
        {
            this.request.setCharacterEncoding("utf-8");
        }
        catch (UnsupportedEncodingException e)
        {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }
    
    public int getInt(String paramName){
        if(request.getParameter(paramName) != null){
            return Integer.parseInt(request.getParameter(paramName));
        }
        return 0;
    }
    
    public long getLong(String paramName){
        if(request.getParameter(paramName) != null){
            return Long.parseLong(request.getParameter(paramName));
        }
        return 0l;
    }
    
    public String getString(String paramName){
        if(request == null){
            return null;
        }
        if(request.getParameter(paramName) != null){
            try
            {
                return new String(request.getParameter(paramName).getBytes("ISO-8859-1"), "gb2312");
            }
            catch (UnsupportedEncodingException e)
            {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
        return null;
    }
    
    public float getFloat(String paramName){
        if(request.getParameter(paramName) != null){
            return Float.parseFloat(request.getParameter(paramName));
        }
        return 0f;
    }
    
    public double getDouble(String paramName){
        if(request.getParameter(paramName) != null){
            return Double.parseDouble(request.getParameter(paramName));
        }
        return 0;
    }
    
    public char getChar(String paramName){
        if(request.getParameter(paramName) != null){
            return request.getParameter(paramName).charAt(0);
        }
        return ' ';
    }
    
}
